package in.muton.edushare.grading.panels.prevgrades;

import in.muton.edushare.documentModel.excel.ExcelUtils;
import in.muton.edushare.grading.panels.prevgrades.old.StudentGradeEdit;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.FilesUploaded;

public abstract class ExcelSheetDisplayPanel extends EdusharePanel {
	private IModel<FilesUploaded> fileModel;
	private List<String[]> fields;
	private int columns;
	private int rows;
	private Log log = LogFactory.getLog(ExcelSheetDisplayPanel.class);

	public ExcelSheetDisplayPanel(String id,
			IModel<FilesUploaded> fileUploadedModel, EduShareBasePage page) {
		super(id, page);
		this.fileModel = fileUploadedModel;
		String path = fileModel.getObject().getFileAbsolutePath();
		try {
			InputStream is = new FileInputStream(path);
			columns = ExcelUtils.getNumColumns(is, 0);
			is.close();
			is = new FileInputStream(path);
			fields = ExcelUtils.readExcelSheet(is, columns, 0);
			is.close();
			is = new FileInputStream(path);
			rows = ExcelUtils.getNumRows(is);
			log.debug("Number of columns = " + columns);
			log.debug("Number of rows = " + rows);
			log.debug("First field = " + fields.get(0));
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		final Form<Void> sform = new Form<Void>("ssform") {
			private static final long serialVersionUID = -4131257961358655262L;

			@Override
			protected void onSubmit() {
				ExcelSheetDisplayPanel.this.onSubmit();
			}
		};
		add(sform);
		sform.setOutputMarkupId(true);
		sform.add(new ListView<String>("thead", getSheetTitles()) {
			private static final long serialVersionUID = -7173312633897220121L;

			@Override
			protected void populateItem(ListItem<String> item) {
				item.add(new Label("headttl", item.getModelObject()));
			}
		});

		sform.add(new ListView<Integer>("rows", StudentGradeEdit.makeList(1,
				rows - 1)) {
			private static final long serialVersionUID = -2668101115032804566L;

			@Override
			protected void populateItem(final ListItem<Integer> arg0) {
				final int row = arg0.getModelObject();
				arg0.add(new Label("sno", "" + arg0.getModelObject()));
				Form<Void> f = new Form<Void>("iform");
				arg0.add(f);
				f.add(new ListView<Integer>("cols", StudentGradeEdit.makeList(
						0, columns - 1)) {
					@Override
					protected void populateItem(ListItem<Integer> arg0) {
						final int col = arg0.getModelObject();
						IModel<String> model = new CellModel(row, col);
						arg0.add(new TextField<String>("data", model));
					}

					private static final long serialVersionUID = 2052233439577481955L;
				});
				f.add(new AjaxSubmitLink("delRow") {
					@Override
					protected void onSubmit(AjaxRequestTarget target, Form<?> f) {
						deleteRow(arg0.getModelObject());
						target.addComponent(sform);
					}

					private static final long serialVersionUID = -1360994473553779689L;
				});
			}
		});

		sform.add(new Link<Void>("cancelInput") {
			private static final long serialVersionUID = 2337638775008708180L;

			@Override
			public void onClick() {
				onCancel();
			}
		});
	}

	private class CellModel extends Model<String> {
		private int row;
		private int col;

		public CellModel(int row, int col) {
			this.row = row;
			this.col = col;
		}

		@Override
		public void setObject(String s) {
			if(fields.size()>row && fields.get(row).length>col)
				fields.get(row)[col] = s;
		}

		@Override
		public String getObject() {
			if(fields.size()>row && fields.get(row).length>col)
				return fields.get(row)[col];
			return "";
		}

		private static final long serialVersionUID = -3668846018791871778L;
	}

	public abstract void onCancel();

	public abstract void onSubmit();

	private List<String> getSheetTitles() {
		List<String> strList = Arrays.asList(fields.get(0));
		strList.set(0, "Roll number/Enrolment ID");
		return strList;
	}

	private void deleteRow(int rowNum) {
		fields.remove(rowNum);
	}
	
	public List<String[]> getFields() {
		return fields;
	}

	public void setFields(List<String[]> fields) {
		this.fields = fields;
	}


	private static final long serialVersionUID = 8310550033476498930L;
}
